<?php
require_once('init.php');

$type_list = array("list","add");

$type = trim($_REQUEST['type']);
if (!in_array($type, $type_list)) $type = "list";

$order_mng = new MerchantOrder();

$html = <<<EOT
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<head>
<title>订单信息</title>
<link rel="stylesheet" href="../common/style.css" type="text/css" />
</head>
<body>
EOT;

$status_list = array(
	1=>'新订单',
	2=>'订单已确认有效',
	3=>'已发货',
	4=>'已支付',
	5=>'已完成',
	6=>'已作废(已取消)'
);

$order_mng = new MerchantOrder();

switch ($type)
{
	case 'list':
		$html .= '<div><a href="../">首页</a></div>';

		$page = $_GET['page'] ? (int)$_GET['page'] : 0;
		$size = $_GET['size'] ? (int)$_GET['size'] : 20;
		$list = $order_mng->getList($page, $size);
		if ($list) {
			$html .= '<table><thead>';
			$html .= '<th>订单ID</th>';
			$html .= '<th>用户ID</th>';
			$html .= '<th>订单金额</th>';
			$html .= '<th>服务费</th>';
			$html .= '<th>优惠金额</th>';
			$html .= '<th>用户支付金额</th>';
			$html .= '<th width="500px">商品详情</th>';
			$html .= '<th>订单状态</th>';
			$html .= '</thead>';
			foreach ($list as $row) {
				$html .= "<tr>";
				$html .= "<td>".$row['id']."</td>";
				$html .= "<td>".$row['uid']."</td>";
				$html .= "<td>￥".$row['order_price']."</td>";
				$html .= "<td>￥".$row['server_price']."</td>";
				$html .= "<td>￥".$row['coupon']."</td>";
				$html .= "<td>￥".$row['total_price']."</td>";
				$html .= "<td>";
				$detail_list = unserialize($row['detail']);
				foreach ($detail_list as $detail) {
					$html .= "商品ID：".$detail[0]."，名称：".$detail[1]."，分类ID：".$detail[2]."，单价：￥".$detail[3]."，购买数量：".$detail[4]."<br/>";
				}
				$html .= "</td><td>".$status_list[$row['status']]."</td></tr>";
			}
			$html .= "</table>";
		} else {
			$html .= '<br/>没有订单信息！';
		}
		break;
	case 'add':
		$html .= '<div><a href="../">首页</a>&nbsp;<a href="?type=list">商品列表</a></div>';

		if (!$_POST['Order'])
		{
			header('Location:?type=list');
		}

		$goods_mng = new MerchantGoods();

		$post = $_POST['Order'];
		$insert_data = array();

		$detail = array();//订单详情
		$server_price = 0;//服务费用，如运送费等
		$coupon = 0;//用户使用优惠卡或积分抵充的金额
		$order_price = 0;//订单金额
		foreach ($post['goods_id'] as $index=>$goods_id) {
			$goods_info = $goods_mng->getOne($goods_id);
			if (!$goods_info)
				continue;

			$name = (string)$goods_info['name'];
			$cid = (int)$goods_info['cid'];
			$price = (float)$goods_info['price'];
			$num = (int)$post['num'][$index];

			$ok = $goods_mng->updateStock($goods_id, 'stock-'.$num);
			if (!$ok)
				continue;

			$order_price += $price*$num;
			$detail[] = array($goods_id,$name,$cid,$price,$num);
		}
		
		//用户实际支付金额
		$total_price = $order_price;
		$total_price += $server_price;
		$total_price -= $coupon;

		$insert_data['detail'] = serialize($detail);
		$insert_data['order_price'] = $order_price;
		$insert_data['server_price'] = $server_price;
		$insert_data['coupon'] = $coupon;
		$insert_data['total_price'] = $total_price;
		$insert_data['status'] = 1;//待支付状态
		$insert_data['order_time'] = time();//下单时间
		$insert_data['updtime'] = time();//订单最后更新时间

		$ok = $order_mng->insert($insert_data);
		if ($ok) {
			// 如果是360CPS过来的用户，保存并反馈订单信息给360CPS系统
			$order_id = $insert_data['id'];
			$cps360_cookie = unserialize($_COOKIE['cps360']);
			if ($order_id && $cps360_cookie)
			{
				foreach ($cps360_cookie as $key=>&$value) {
					$value = urldecode($value);
				}

                require_once('../cps360/cps360.config.php');//360cps对接配置
                require_once('../cps360/cps360.class.php');//360cps对接的处理类
				$cps360_order_mng = new Cps360Order();
				$cps360_order_info = array();
				$cps360_order_info['order_id'] = $order_id;
				$cps360_order_info['bid'] = $cps360_cookie['bid'];
				$cps360_order_info['qihoo_id'] = $cps360_cookie['qihoo_id'];
				$cps360_order_info['url'] = $cps360_cookie['url'];
				$cps360_order_info['from'] = $cps360_cookie['from'];
				$cps360_order_info['cookie_time'] = $cps360_cookie['cookie_time'];
				$cps360_order_info['ext'] = $cps360_cookie['ext'];
				$cps360_order_info['qid'] = $cps360_cookie['qid'];
				$cps360_order_info['qmail'] = $cps360_cookie['qmail'];
				$cps360_order_info['qname'] = $cps360_cookie['qname'];
				$ok = $cps360_order_mng->insert($cps360_order_info);
				if ($ok) {
					// 订单反馈
					$cps360_order_id = $cps360_order_info['id'];
					$cps360_order_mng->feedback($cps360_order_id);
				}
			}
		}

		header('Location:?type=list');
		break;
}

$html .= <<<EOT
</body>
</html>
EOT;

echo $html;
?>
